package org.dearsor.elective.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ognl.SetPropertyAccessor;

import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.dearsor.elective.exception.CreditOverflowExeption;
import org.dearsor.elective.exception.NotLoggedInException;
import org.dearsor.elective.model.Course;
import org.dearsor.elective.model.Person;
import org.dearsor.elective.model.Student;
import org.dearsor.elective.service.CourseMgr;
import org.dearsor.elective.service.StudentMgr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Component
//@ResultPath("/WEB-INF/views")
@Results({
	@Result(name = "course-list", location = "courseList.jsp"),
	@Result(name = "courseList",location = "courseList",type = "redirectAction"),
	@Result(name = "error", location = "error.jsp"),
	@Result(name = "creditOverflow", location = "creditOverflow.jsp")
})
@ExceptionMappings({
	@ExceptionMapping(exception="org.dearsor.elective.exception.NotLoggedInException",
			  result = "needLogin"),
	@ExceptionMapping(exception="java.lang.Exception",result = "needLogin")
})

public class CourseAction extends ActionSupport 
						implements Preparable,
								   SessionAware
//								   ServletRequestAware, 
//								   ServletResponseAware 
								   {
	
	

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -1609594868141383147L;

	@Autowired 
	private CourseMgr courseMgr;
	
	@Autowired
	private StudentMgr studentMgr;
	
	private Person person;
	
	private List<Course> availableCourseList;
	
	private List<Course> selectedCourseList;
	
	private long courseId;
	
	 private Map<String, Object> attr;  
	 
	 
	 @org.apache.struts2.convention.annotation.Action("/courseList")
	 public String courseList() {
		 return "course-list";
	 }
	 
	 @org.apache.struts2.convention.annotation.Action("/select")
	 public String select() throws NotLoggedInException {
		 try {
			checkLogin();
			 courseMgr.doSelect(courseId,person);
			 return "courseList";
		} catch (DataAccessException e) {
			e.printStackTrace();
			return "error";
		} catch (CreditOverflowExeption e) {
			e.printStackTrace();
			return "creditOverflow";
		}
	 }
	 
	 
	 @org.apache.struts2.convention.annotation.Action("/unselect")
	 public String unselect() throws NotLoggedInException {
		 try {
			checkLogin();
			 courseMgr.doUnselect(courseId,person);
			 return "courseList";
		} catch (DataAccessException e) {
			
			e.printStackTrace();
			return "error";
		}
	 }
	 


	public void prepare() throws Exception {
		
		checkLogin();
		Student student = getStudentMgr().find(Student.class, getPerson().getId());
		setAvailableCourseList(getCourseMgr().listAvailableCourses(student));
		setSelectedCourseList(student.getCourses());
	}

	/**
	 * @throws NotLoggedInException
	 */
	protected void checkLogin() throws NotLoggedInException {
		setPerson((Person) getAttr().get("currentUser"));
		if (getPerson() == null) {
			throw new NotLoggedInException();
		}
	}

	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.attr = arg0;
	}



	

/*	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
	}

	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
*/
	
	public StudentMgr getStudentMgr() {
		return studentMgr;
	}
	
	public CourseMgr getCourseMgr() {
		return courseMgr;
	}



	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public List<Course> getAvailableCourseList() {
		return availableCourseList;
	}

	public void setAvailableCourseList(List<Course> availableCourseList) {
		this.availableCourseList = availableCourseList;
	}

	public List<Course> getSelectedCourseList() {
		return selectedCourseList;
	}

	public void setSelectedCourseList(List<Course> selectedCourseList) {
		this.selectedCourseList = selectedCourseList;
	}

	public Map<String, Object> getAttr() {
		return attr;
	}

	public void setAttr(Map<String, Object> attr) {
		this.attr = attr;
	}

	public void setCourseId(long courseId) {
		this.courseId = courseId;
	}

	public long getCourseId() {
		return courseId;
	}
}
